<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;
use think\facade\Db;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class Goods extends Model
{
    use SoftDelete;
    /**
     * 获取表字段
     * @param bool $isPk 是否获取主键
     *
     * @return array
     */
    public function getFields(bool $isPk=false):array {
        $fields = array_keys(Db::table($this->getTable())->getFields());
        if(!$isPk){
            $pk = $this->getPk();
            foreach ($fields as $k=>$f){
                if($f === $pk){
                    unset($fields[$k]);
                }
            }
        }

        return $fields;
    }

    public function setCoverAttr($value): string
    {
        return $value ? removeDomain($value):'';
    }

    public function getCoverAttr($value): string
    {
        return $value ? addDomain($value):'';
    }
    public function setVideoAttr($value): string
    {
        return $value ? removeDomain($value):'';
    }

    public function getVideoAttr($value): string
    {
        return $value ? addDomain($value):'';
    }

    public function setImagesAttr($value): string
    {
        $data = is_array($value)?$value:($value?explode(',',$value):[]);
        if(count($data)){
            $data = array_map('removeDomain',$data);
        }
        return count($data)?serialize($data):'';
    }

    public function getImagesAttr($value): array
    {
        $data = $value?unserialize($value):[];
        if(count($data)){
            $data = array_map('addDomain',$data);
        }
        return $data;
    }

    public function c(){
        return $this->belongsTo('GoodsClassify','goods_classify_id','id')->bind(['classify_name'=>'name']);
    }
    //商品详情
    public function dt(){
        return $this->hasOne('GoodsDetail','goods_id');
    }
    //商品参数
    public function ps(){
        return $this->hasMany('GoodsParam','goods_id');
    }
    //商品多规格
    public function items(){
        return $this->hasMany('GoodsItem','goods_id');
    }
    //商品规格
    public function spec(){
        return $this->hasMany('GoodsSpec','goods_id');
    }

}
